home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / Queue.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  5KB  |  178 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from time import time as _time
  5. from collections import deque
  6. __all__ = [
  7.     'Empty',
  8.     'Full',
  9.     'Queue']
  10.  
  11. class Empty(Exception):
  12.     pass
  13.  
  14.  
  15. class Full(Exception):
  16.     pass
  17.  
  18.  
  19. class Queue:
  20.     
  21.     def __init__(self, maxsize = 0):
  22.         
  23.         try:
  24.             import threading as threading
  25.         except ImportError:
  26.             import dummy_threading as threading
  27.  
  28.         self._init(maxsize)
  29.         self.mutex = threading.Lock()
  30.         self.not_empty = threading.Condition(self.mutex)
  31.         self.not_full = threading.Condition(self.mutex)
  32.         self.all_tasks_done = threading.Condition(self.mutex)
  33.         self.unfinished_tasks = 0
  34.  
  35.     
  36.     def task_done(self):
  37.         self.all_tasks_done.acquire()
  38.         
  39.         try:
  40.             unfinished = self.unfinished_tasks - 1
  41.             if unfinished <= 0:
  42.                 if unfinished < 0:
  43.                     raise ValueError('task_done() called too many times')
  44.                 
  45.                 self.all_tasks_done.notifyAll()
  46.             
  47.             self.unfinished_tasks = unfinished
  48.         finally:
  49.             self.all_tasks_done.release()
  50.  
  51.  
  52.     
  53.     def join(self):
  54.         self.all_tasks_done.acquire()
  55.         
  56.         try:
  57.             while self.unfinished_tasks:
  58.                 self.all_tasks_done.wait()
  59.         finally:
  60.             self.all_tasks_done.release()
  61.  
  62.  
  63.     
  64.     def qsize(self):
  65.         self.mutex.acquire()
  66.         n = self._qsize()
  67.         self.mutex.release()
  68.         return n
  69.  
  70.     
  71.     def empty(self):
  72.         self.mutex.acquire()
  73.         n = self._empty()
  74.         self.mutex.release()
  75.         return n
  76.  
  77.     
  78.     def full(self):
  79.         self.mutex.acquire()
  80.         n = self._full()
  81.         self.mutex.release()
  82.         return n
  83.  
  84.     
  85.     def put(self, item, block = True, timeout = None):
  86.         self.not_full.acquire()
  87.         
  88.         try:
  89.             if not block:
  90.                 if self._full():
  91.                     raise Full
  92.                 
  93.             elif timeout is None:
  94.                 while self._full():
  95.                     self.not_full.wait()
  96.             elif timeout < 0:
  97.                 raise ValueError("'timeout' must be a positive number")
  98.             
  99.             endtime = _time() + timeout
  100.             while self._full():
  101.                 remaining = endtime - _time()
  102.                 if remaining <= 0:
  103.                     raise Full
  104.                 
  105.                 self.not_full.wait(remaining)
  106.             self._put(item)
  107.             self.unfinished_tasks += 1
  108.             self.not_empty.notify()
  109.         finally:
  110.             self.not_full.release()
  111.  
  112.  
  113.     
  114.     def put_nowait(self, item):
  115.         return self.put(item, False)
  116.  
  117.     
  118.     def get(self, block = True, timeout = None):
  119.         self.not_empty.acquire()
  120.         
  121.         try:
  122.             if not block:
  123.                 if self._empty():
  124.                     raise Empty
  125.                 
  126.             elif timeout is None:
  127.                 while self._empty():
  128.                     self.not_empty.wait()
  129.             elif timeout < 0:
  130.                 raise ValueError("'timeout' must be a positive number")
  131.             
  132.             endtime = _time() + timeout
  133.             while self._empty():
  134.                 remaining = endtime - _time()
  135.                 if remaining <= 0:
  136.                     raise Empty
  137.                 
  138.                 self.not_empty.wait(remaining)
  139.             item = self._get()
  140.             self.not_full.notify()
  141.             return item
  142.         finally:
  143.             self.not_empty.release()
  144.  
  145.  
  146.     
  147.     def get_nowait(self):
  148.         return self.get(False)
  149.  
  150.     
  151.     def _init(self, maxsize):
  152.         self.maxsize = maxsize
  153.         self.queue = deque()
  154.  
  155.     
  156.     def _qsize(self):
  157.         return len(self.queue)
  158.  
  159.     
  160.     def _empty(self):
  161.         return not (self.queue)
  162.  
  163.     
  164.     def _full(self):
  165.         if self.maxsize > 0:
  166.             pass
  167.         return len(self.queue) == self.maxsize
  168.  
  169.     
  170.     def _put(self, item):
  171.         self.queue.append(item)
  172.  
  173.     
  174.     def _get(self):
  175.         return self.queue.popleft()
  176.  
  177.  
  178.